package tienmanh.gs.goldminer.sprite;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.IModifier;

import tienmanh.gs.goldminer.manager.ResourcesManager;
import tienmanh.gs.goldminer.manager.SceneManager;
import tienmanh.gs.goldminer.scene.GameScene;
import tienmanh.gs.goldminer.util.Value;

public class MouseBeat extends Object{

	  boolean isCarry;
//	private  int value;
	  boolean isDetach;
	private  boolean isHuong;
	private AnimatedSprite sprite;
	private VertexBufferObjectManager vbom;
//	public boolean left , right;
		
		  @Override
		protected void onManagedUpdate(float pSecondsElapsed) {
			// TODO Auto-generated method stub
			super.onManagedUpdate(pSecondsElapsed);
			if(!GameScene.isFrzee && !isCheckMouseRun){
				if(end1 > start1)
					if(getX() > end1 || getX() < start1 ){
						speed = -speed;
						isHuong = !isHuong;
						if(isHuong) animate(new long[] { 100L, 100L}, new int[]{0,1}, true);
						 else      animate(new long[] { 100L, 100L}, new int[]{4,5}, true);
					}
					if(end1 < start1){
						if(getX() < end1 || getX() > start1 ){
							speed = -speed;
							isHuong = !isHuong;
							if(!isHuong) animate(new long[] { 100L, 100L}, new int[]{0,1}, true);
							 else      animate(new long[] { 100L, 100L}, new int[]{4,5}, true);
						}	
					}
					setPosition(getX()+speed, getY());
			}
			if(!isAnimationRunning())
				if(isHuong) 
					 animate(new long[] { 100L, 100L}, new int[]{0,1}, true);
				 else      
					 animate(new long[] { 100L, 100L}, new int[]{4,5}, true);
			if(!isCarry)
			{
			for (int i = 0; i < GameScene.arrayGolds.size(); i++) {
				if(GameScene.arrayGolds.get(i)!=null)
				if(this.collidesWith(GameScene.arrayGolds.get(i))){
					if(GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_GOLD_2) ||
						    GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_GOLD_5) || 
				    		GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_STONE_2) ||
				    			GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_JEWEL_0)||
				    			GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_JEWEL_1)||
				    			GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_JEWEL_2)||
				    			GameScene.arrayGolds.get(i).getType().equals(new Value().TYPE_JEWEL_3)){
						isCarry = true;
						isDetach = true;
							if(isHuong)
								animate(new long[]{60,60,60}, new int[]{0,1,2} , false);
								else
								animate(new long[]{60,60,60}, new int[]{3,4,5} , false);	
						this.sprite = new Gold(GameScene.arrayGolds.get(i).getX(), GameScene.arrayGolds.get(i).getY(),GameScene.arrayGolds.get(i).getWidth(),GameScene.arrayGolds.get(i).getHeight(), GameScene.arrayGolds.get(i).getTiledTextureRegion(),vbom,0, 0, "");
						GameScene.getInstace().sceneHUD.detachChild(GameScene.arrayGolds.get(i));
						GameScene.getInstace().sceneHUD.attachChild(GameScene.arrayGolds.get(i));
						GameScene.sceneHUD.detachChild(GameScene.arrayGolds.get(i));
						GameScene.arrayGolds.set(i, null);
						GameScene.sceneHUD.attachChild(sprite);
						final SequenceEntityModifier entityModifier = new SequenceEntityModifier(
								new IEntityModifierListener() {
									@Override
									public void onModifierStarted(
											final IModifier<IEntity> pModifier,
											final IEntity pItem) {
									}

									@Override
									public void onModifierFinished(
											final IModifier<IEntity> pEntityModifier,
											final IEntity pEntity) {
										try {
//											GameScene.sceneHUD.detachChild(sprite);
											isCarry = false;
											isDetach = false;
										} catch (Exception e) {

										}
									}
								}, new AlphaModifier(2, 1f, 0f));
						sprite.registerEntityModifier(entityModifier);
						break;
					}
				}
			}
			}
			
		}
		  @Override
			public void pullBackGold(float paramFloat1, float paramFloat2,
					float paramFloat3, float paramFloat4, boolean paramBoolean) {
				// TODO Auto-generated method stub
				super.pullBackGold(paramFloat1, paramFloat2, paramFloat3, paramFloat4,
						paramBoolean);
				stopAnimation();
		    	unPigRun();
		    	isCarry = true;
		    	isCheckMouseRun = true;
			}
			private SequenceEntityModifier lemRun;
			public MouseBeat(float pX, float pY, float pWidth, float pHeight,
					ITiledTextureRegion pTiledTextureRegion,
					VertexBufferObjectManager pVertexBufferObjectManager, int paramInt,
					float paramFloat5, String paramString) {
				super(pX, pY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager,
						paramInt, paramFloat5, paramString);
				// TODO Auto-generated constructor stub
				 isCarry = false;
				 isDetach = false;
						 vbom = pVertexBufferObjectManager;
//						 left = false;
//						 right = false;
			}
			public float start1,start2,end1,end2,speed;
			private  boolean isCheckMouseRun;
			//huong = true phai sang trai
			 public void pigRun(final float paramFloat1, final float paramFloat2, final float paramFloat3, final float paramFloat4, final float paramFloat5, final boolean huong)
			  {
				 end1 = paramFloat3;
				 end2 = paramFloat4;
				 speed = paramFloat5;
				 start1 = paramFloat1;
				 start2 = paramFloat2;
				 isHuong = huong;
				 if(huong) {
					 System.out.println(1);
					 animate(new long[] { 100L, 100L}, new int[]{0,1}, true);
				 }
				 else
				 {
					 System.out.println(2);
					 animate(new long[] { 100L, 100L}, new int[]{4,5}, true);
				 }
			  }
			 public void unPigRun()
			  {
			    unregisterEntityModifier(this.lemRun);
			  }

	
}
